/*
 * Class: 	CFont
 * Author: 	TeaM
 * Descp: 	Bitmap Font
 * 
 * class dung de ve 1 chuoi bang font bitmap ra man hinh
 * align: 0-canh trai, 1-canh phai, 2-canh giua
 * */

import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class CFont 
{
	private Image imgFont;
	private String stMap;
	private String stMap2;
	private byte[] charW;
	private int charH;
	private int spaceW;
	private int imgWidth;
	
	private int pos;

	//dw: default char width
	//cw: char width array
	//ch: char height
	//sw: space width
	//map: string map 1
	public CFont(String name,byte[] charwidth,int height,int spacewidth,String map)
	{
		try
		{
             imgFont=Image.createImage("/"+name);
		}
		catch (Exception e)	{}
		charW=charwidth;
		charH=height;
		spaceW=spacewidth;
		stMap=map;
		imgWidth=imgFont.getWidth();
		
		if (charwidth.length != map.length())
			System.out.println("Font '"+name+"' error!!! "+charwidth.length+"-"+map.length());
	}

	public int stringWidth(byte[] st)
	{
		int width = 0;
		for(int i=0;i<st.length;i++)
		{
			if (st[i]>=0)
				width+=charW[st[i]];
			else
				width+=spaceW;
		}
		return width;
	}
	
	public int substringWidth(byte[] st,int start,int len)
	{
		int width = 0;
		int end=start+len;
		if (end>st.length) end=st.length;
		for(int i=start;i<end && st[i]!=-3;i++)
		{
			if (st[i]>=0)
				width+=charW[st[i]];
			else
				width+=spaceW;
		}
		return width;
	}
	
	public int stringWidth(String st)
	{
		return substringWidth(st,0,st.length());
	}
	
	public int substringWidth(String st,int start,int len)
	{
		int width = 0;
		int end=start+len;
		if (end>st.length()) end=st.length();
		
		for(int i=start;i<end;i++)
		{
			char ch=st.charAt(i);
			if (ch=='\n') break;
			width+=charWidth(ch);
		}
		return width;
	}
	
	// unknow: -1
	// ' '   : -2
	// '\n'  : -3
	public byte[] toBytes(String st)
	{
		byte data[]=new byte[st.length()];
		for(int i=0;i<data.length;i++)
		{
			if (st.charAt(i)==' ') data[i]=-2;
			else if (st.charAt(i)=='\n') data[i]=-3;
			else
			{
				data[i]=(byte)stMap.indexOf(st.charAt(i));
				if (data[i]==-1 && stMap2!=null) data[i]=(byte)stMap2.indexOf(st.charAt(i));
			}
		}
		return data;
	}

	
	
	// Viet chu co gach duoi
	// 1: gach duoi
	// 2: gach giua
	public void drawStringL(String st,int x,int y,int align,int line,Graphics g)
	{
		int x1=x,y1=y;
		
		if (align==0) x1=x;
		else if (align==1) x1=x-stringWidth(st);
		else x1=x-(stringWidth(st)>>1);
		if ((align&Graphics.LEFT) !=0)
                    x1=x;
		else if ((align&Graphics.RIGHT)!=0)
                    x1=x-stringWidth(st);
		else if ((align&Graphics.HCENTER)!=0)
                    x1=x-(stringWidth(st)>>1);

                if((align&Graphics.TOP)!=0)
                    y1=y;
                else if((align&Graphics.BOTTOM)!=0)
                    y1=y-charH;
                else if((align&Graphics.VCENTER)!=0 || (align&Graphics.BASELINE)!=0)
                    y1=y-(charH>>1);
		if (line==1)
		{
			g.drawLine(x1,charH+y,x1+stringWidth(st)-1,charH+y);
		}
		drawString(st,x1,y1,0,g);
		if (line==2)
		{
			g.drawLine(x1,charH/2+y,x1+stringWidth(st)-1,charH/2+y);
		}
	}
	
	public void drawString(String st,int x,int y,int align,Graphics g)
	{
		drawSubstring(st,0,st.length(),x,y,align,g);
	}
	
	public void drawSubstring(String st,int start,int len,int x,int y,int align,Graphics g)
	{
		int x1=x,y1=y,pos,end=start+len;
		if (end>st.length()) end=st.length();
		
		if ((align&Graphics.LEFT) !=0) x1=x;
		else if ((align&Graphics.RIGHT)!=0) x1=x-substringWidth(st,start,len);
		else if ((align&Graphics.HCENTER)!=0) x1=x-(substringWidth(st,start,len)>>1);

        if((align&Graphics.TOP)!=0)
            y1=y;
        else if((align&Graphics.BOTTOM)!=0)
            y1=y-charH;
        else if((align&Graphics.VCENTER)!=0 || (align&Graphics.BASELINE)!=0)
            y1=y-(charH>>1);
		char ch;
		for(int i=start;i<end;i++)
		{
			ch=st.charAt(i);
			if (ch=='\n')
			{
				if (align==0) x1=x;
				else if (align==1) x1=x-substringWidth(st,i+1,len-i-start-1);
				else x1=x-(substringWidth(st,i+1,len-i-start-1)>>1);
				y1+=charH;
			}
			else if ((pos=charIndex(ch))>=0)
			{
				g.drawRegion(imgFont,0,pos*charH,imgWidth,charH,0,x1,y1,20);
				x1+=charW[pos];
			}
			else
			{
				x1+=spaceW;
			}
		}
	}
	
	public void drawChar(char ch,int x,int y,int align,Graphics g)
	{
		if ((pos=charIndex(ch))>=0)
		{
			g.drawRegion(imgFont,0,pos*charH,imgWidth,charH,0,x,y,0);
		}
	}
	
	public void drawString(byte[] st,int x,int y,int align,Graphics g)
	{
		drawSubstring(st, 0, st.length, x, y, align, g);
	}
	
	public void drawSubstring(byte[] st,int start,int len,int x,int y,int align,Graphics g)
	{
		int x1,y1,end=start+len;
		if (end>st.length) end=st.length;
		
		if (align==0) x1=x;
		else if (align==1) x1=x-substringWidth(st,start,len);
		else x1=x-(substringWidth(st,start,len)>>1);
		y1=y;
		
		for(int i=start;i<end;i++)
		{
			if (st[i]==-3)
			{
				if (align==0) x1=x;
				else if (align==1) x1=x-substringWidth(st,i+1,len-i-start-1);
				else x1=x-(substringWidth(st,i+1,len-i-start-1)>>1);
				y1+=charH;
			}
			else if (st[i]>=0)
			{
				g.drawRegion(imgFont,0,st[i]*charH,imgWidth,charH,0,x1,y1,20);
				x1+=charW[st[i]];
			}
			else
			{
				x1+=spaceW;
			}
		}
	}
	
	public int getHeight()
	{
		return charH;
	}
	
	public int charIndex(char ch)
	{
		return stMap.indexOf(ch);
	}
	
	public int charWidth(char ch)
	{
		pos=charIndex(ch);
		if (pos!=-1) return charW[pos];
		return spaceW;
	}
	
	// Cat 1 string dai thanh nhieu string nho voi do rong toi da width
	public String[] cut(String st,int width)
	{
		Vector vector=new Vector();
		int i=0,i1=0,w1=0,ispace=-1;
		char ch;
		while (i<st.length())
		{
			ch=st.charAt(i);
			if (ch==' ') ispace=i;
			w1+=charWidth(ch);
			if (w1>width || ch=='\n')
			{
				if (ch!='\n' && ispace!=-1) i=ispace;	
				vector.addElement(st.substring(i1, i));
				if (st.charAt(i)==' ' || ch=='\n') i++;
				i1=i;
				w1=0;
				ispace=-1;
			}
			else i++;
		}
		if (i>i1) vector.addElement(st.substring(i1, i)); // end line
		
		String[] a=new String[vector.size()];
		for (i=0;i<a.length;i++)
		{
			a[i]=(String)vector.elementAt(i);
		}
		return a;
	}
}
